package br.ufc.cloud;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

public class EditarUser extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String idUserString = req.getParameter("idUser");
		Entity userEntity = buscarUserPorId(Integer.parseInt(idUserString));
		if (userEntity != null) {
			resp.getWriter().print(userEntity.getProperty("name"));
			resp.getWriter().print(userEntity.getProperty("email"));
		}
	}

	public Entity buscarUserPorId(int idUser) {

		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();

		Filter filterName = new FilterPredicate("id", FilterOperator.EQUAL,
				idUser);
		Entity user = null;
		
		Query query = new Query("User").setFilter(filterName);
		PreparedQuery preparedQuery = datastore.prepare(query);
		user = preparedQuery.asSingleEntity();

		return user;
		

		
	}
}
